﻿@model PagedList.IPagedList<CP_SchoolForChildren_v0._1.Models.RelatedModel.QAModel>
@using CP_SchoolForChildren_v0._1.Models.DataModel;
@{
    ViewBag.Title = "Hỏi đáp mỗi ngày";
    Layout = "~/Views/Shared/FE/_Layout_FE_Index.cshtml";
    SFCEntities db = new SFCEntities();

    var questionlist = ViewBag.questionlist;
    List<Question> viewquestionlist = ViewBag.questionlist;
    var count = ViewBag.Count;
    string search = ViewBag.searchstring;
    @ViewBag.Message
}
<!-- Javascript -->
<script type="text/javascript">
    $(document).ready(function () {
        $(".delete-qa-button").click(function (event) {
            event.preventDefault();
            var targetUrl = $(this).attr("href");
            
            $("#dialog-confirm").dialog({
                resizable: false,
                height: 180,
                modal: true,
                buttons: {
                    "Có": function () {
                        $(this).dialog("close");
                        window.location.href = targetUrl;
                    },
                    "Không": function () {
                        $(this).dialog("close");
                    }
                }
            });
        });
        $("#QuestionContent").keyup(function () {
            var max = 1000;
            var len = $(this).val().length;
            //$("#chtcount").html(max - len);
            if (len > max) {
                $("#postQ").attr({ disabled: 'disabled' });
                $("#chtcount").html("Trên 1000 kí tự");
            } else {
                $("#postQ").removeAttr("disabled");
                $("#chtcount").html(len + " kí tự");
            }
            if (len == 0)
                $("#postQ").attr({ disabled: 'disabled' });

        });
        $('#qform').submit(function () {
            var len = $("#QuestionContent").val().length;
            if (len >= 30) {
                return true;
            } else if (len < 30) {
                $("#dialog-message").dialog({
                    modal: true,
                    resizable: false,
                    buttons: {
                        Ok: function () {
                            $(this).dialog("close");
                        }
                    }
                });
                return false;
            }
            return false;
        });

        $('#search-form').submit(function (e) {
            if (!$('#searchString').val() ||
                $('#searchString').val().trim() == null ||
                $('#searchString').val().trim() == "") {
                e.preventDefault();
            }
        });
    });
    function transer() {
        var s = $("#qabutton").html();
        if (s == "Hỏi") {
            $("#qabutton").html("Đáp");
        } else {
            $("#qabutton").html("Hỏi");
        }
        $(".topqa").toggle();
    };
</script>
<!-- Import -->
<link href="@Url.Content("~/Content/FE/Addition.css")" rel="stylesheet" type="text/css" />
<!-- MessageBox -->
<div id="dialog-message" class="ui-dialog-content ui-widget-content" style="display: none;" title="Có lỗi xảy ra">
    <p>
        <span class="ui-icon ui-icon-circle-check" style="float: left; margin: 0 7px 50px 0;"></span>
        Nội dung câu hỏi cần có 30 ký tự trở lên
    </p>
</div>
<!-- Render -->
<div class="allhome" style="padding-top: 15px;">
    <!-- Left bar start -->
    <div class="dashboard">
        @if (User.Identity.Name != "")
        {
            <div class="content-header">
                <div class="header-inner" style="box-shadow: none">
                    <h2>@User.Identity.Name</h2>
                </div>
                <br class="spacer"/>
                <div class="">
                    <ul class="stats">
                        @{

                            string name = User.Identity.Name;
                            var user = from u in db.Users
                                       where (u.UserName == name)
                                       select u;
                            int userid = user.First().UserID;

                            var question = from q in db.Questions
                                           where q.UserID == userid
                                           select q.QuestionID;
                            int numberofquestion = question.Count();


                            var answer = from a in db.Answers
                                         where a.UserID == userid
                                         select a.UserID;
                            int NumberOfAnswer = answer.Count();
                            <li><a href= '@Url.Action("ViewUserQuestion", "QA", new {id = userid})'><strong>@numberofquestion</strong> Câu hỏi</a></li>
                            <li><a href = '@Url.Action("ViewUserAnswer", "QA", new {id = userid})' style="border-left: 1px solid rgba(119, 79, 56, 0.2);"><strong>@NumberOfAnswer</strong> Câu trả lời</a></li>
                        }
               
                
                    </ul>
                </div>
                <div class="tweet-user">
                    @if (Request.IsAuthenticated)
                    {
                        <form action="@Url.Action("Create", "QA")" id="qform" method="post">
                            <script type="text/javascript"> </script>
                            <div>
                                <textarea id="QuestionContent" name ="QuestionContent" class="tweet-box" dir="ltr"placeholder="Nhập nội dung câu hỏi mới"></textarea>
                            </div>
                            <div class="tweet-button">
                                <button disabled="disabled" id="postQ" type="submit" class="btn">Gửi câu hỏi mới</button>
                                <p id="chtcount" style="float: right; opacity: 0.4;">0 Kí tự</p>
                            </div>
                            <br class="spacer"/>
                        </form>
                    }
                </div>
            </div>
            <div style="padding-top: 5px; padding-bottom: 5px"></div>
        }
        <div class="content-header">
            <div class="tweet-user" style="border-bottom: 1px solid #E8E8E8;">
            </div>
            <div class="header-inner">
                <h2>@Html.ActionLink("Câu hỏi mới nhất", "Index", "QA")</h2>
                <br class="spacer"/>
                @{
                    int j = 0;
                    for (int i = questionlist.Count; i > questionlist.Count - 5; i--)
                    {
                        if (i >= 1)
                        {
                            j = j + 1;
                            <div class="ranktopquestion">
                                <div class="STT">
                                    <p>@j</p>
                                </div>
                                <div class="NewQuestion">
                                    @if (viewquestionlist[i - 1].QuestionContent.Length > 100)
                                    {
                                        string cutContent = viewquestionlist[i - 1].QuestionContent.Substring(0, 100) + "... ";
                                        @Html.ActionLink(cutContent, "Details", new {id = viewquestionlist[i - 1].QuestionID})
                                    }
                                    else
                                    {
                                        @Html.ActionLink(viewquestionlist[i - 1].QuestionContent, "Details", new {id = viewquestionlist[i - 1].QuestionID})
                                    }
                                    
                                </div>
                                <div style="clear: both"></div>
                            </div>
                        }
                    }
                }
            
            
            </div>
    
        </div>
        <div class="content-header" style="margin-top: 10px;">
            <script type="text/javascript">
                
            </script>
            <div class="tweet-user">
            </div>
            <div class="header-inner">
                <h3 style="padding-left: 15px; cursor: pointer; float: left;" >Thành viên tích cực</h3>
                <button id="qabutton" style="float: right; margin-right: 15px;" type="button" onclick=" transer() " class="btn">Hỏi</button>
                <div style="clear: both; padding-top: 15px;"></div>
            
                @{
                    var questioncount = from q in db.Questions
                                        group q by q.UserID
                                        into g
                                        select new {userkey = g.Key, quescount = g.Count()};
                    var maxquestionlist = questioncount.OrderByDescending(q => q.quescount).Take(10).ToList();
                    for (int i = maxquestionlist.Count - 10; i <= maxquestionlist.Count; i++)
                    {
                        if (i >= 1)
                        {
                            int userID = maxquestionlist[i - 1].userkey;
                            string username = (db.Users.Where(u => u.UserID == userID)).SingleOrDefault().UserName;
                            int maxquestion = maxquestionlist[i - 1].quescount;
                            <div class="topqa">
                                <div class="topqaname">
                                    <a href='@Url.Action("ViewUserProfile", "User", new {id = userID})'>@username</a>
                                </div>
                                <div class="topqacount" style="border-left: 1px solid #E8E8E8;">
                                    @Html.ActionLink(maxquestion.ToString(), "ViewUserQuestion", new {id = userID})
                                </div>
                                <div style="clear: both"></div>
                            </div>
                        }
                    }

                    var commentcount = from c in db.Answers
                                       group c by c.UserID
                                       into g
                                       select new {usercommentID = g.Key, comcount = g.Count()};
                    var maxcommentlist = commentcount.OrderBy(q => q.comcount).ToList();

                    for (int i = maxcommentlist.Count; i >= maxcommentlist.Count - 10; i--)
                    {
                        if (i >= 1)
                        {
                            int usercommentID = maxcommentlist[i - 1].usercommentID;
                            string usernamecomment = (db.Users.Where(u => u.UserID == usercommentID)).SingleOrDefault().UserName;
                            int maxcomment = maxcommentlist[i - 1].comcount;
                            <div class="topqa" style="display: none;">
                                <div class="topqaname">
                                    <a href='@Url.Action("ViewUserProfile", "User", new {id = usercommentID})'>@usernamecomment</a>
                                </div>
                                <div class="topqacount" style="border-left: 1px solid #E8E8E8;">
                                    @Html.ActionLink(@maxcomment.ToString(), "ViewUserAnswer", new {id = usercommentID})
                                </div>
                                <div style="clear: both"></div>
                            </div>
                        }
                    }


                }
            </div>
        </div> 
    </div>
    <!-- Left bar end -->

    <!-- Content -->
    <div class="content-main">
        <div class="content-header">
            <div class="header-inner">
                <div style="border-bottom: 1px solid rgba(119, 79, 56, 0.4);">
                    <h2>Câu hỏi</h2>
                    <div style="float: right; width: 80%">
                        <form style="float: right;" method="POST" id="search-form" action="@Url.Action("Index", "QA")">
                            <input type="text" class="searchInput" name="searchString" id="searchString" placeholder="Nhập nội dung câu hỏi để tìm kiếm" value ="@search" style="width: 250px;"/>            
                            <input type="submit" id="searchSubmit" value="Tìm" style="margin-left: 5px; margin-right: 5px;" />
                        </form>
                        @if (count > 0)
                        {
                            <br class="spacer" />
                            <span style="float: right; padding: 5px;">Tìm thấy <b>@count</b> kết quả</span>
                        }              
                    </div>
                    <br class="spacer"/>
                </div>
            </div>
            @foreach (var item in Model)
            {
                <div class="questioncontent">
                    <div class="content">
                        <div class="stream-item-header">
                            <small class="time">
                                @{
                                    if (User.Identity.Name != "")
                                    {
                                        var role = db.UserRoles.Single(ur => ur.RoleName == "Admin");
                                        string name = User.Identity.Name;
                                        var user = from u in db.Users
                                                   where (u.UserName == name)
                                                   select u;
                                        int userid = user.First().UserID;
                                        if (userid == item.UserID || user.First().RoleID == role.RoleID)
                                        {
                                            @Html.ActionLink("ｘ", "DeleteQuestion", new { id = item.QuestionID }, new { @class = "delete-qa-button" })
                                        }
                                    }
                                }
                            </small>
                            <a href="@Url.Action("ViewUserProfile", "User", new {id = @item.UserID})" class="account-group">
                                <strong class="fullname" style="float: left; padding-bottom: 5px;">@item.UserName</strong>
                                <br />
                                @if (string.IsNullOrEmpty(item.AvatarURL))
                                {
                                    item.AvatarURL = @Url.Content("~/AvartarUser/default_avt.jpg");
                                }
                                @Html.DisplayFor(modelItem => item.AvatarURL)
                                                      
                                                      
                            </a>
                                              
                            <div class="q_title">
                                <div class="contentdetail">
                                    <p style="color: #BBBBBB; font-size: 12px; font-style: italic">Hỏi: </p>
                                    @Html.ActionLink(item.QuestionContent, "Details", new {id = item.QuestionID})
                                </div>
                            </div>
                            <div style="clear: both"></div>
                            @{
                                var answers = from a in db.Answers
                                              where a.QuestionID == item.QuestionID
                                              select a.AnswerID;

                            }
                            <small class="time"><p style="font-style: italic; color: #2f3a52">Gửi lúc: @item.CreatedTime | Trả lời: @answers.Count()</p></small>
                            <div style="clear: both"></div>
                            <div class="time" style="padding-top: 5px;"><button class="btn" type="button"onclick=" location.href = '@Url.Action("Details", "QA", new {id = item.QuestionID})' ">Trả lời</button></div>
                            <div style="clear: both"></div>
                        </div>
                    </div>
                </div>
            }
            <div class="questioncontent">
                <div class="paging">
                    @if (Model.PageCount == 0)
                    {
                        if (count == 0)
                        {
                            <span>Không tìm thấy kết quả phù hợp</span>
                        }
                    }
                    else if (Model.PageCount != 0)
                    {
                        <span>Trang <span style="font-weight: bold">@(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber)</span>
                            / <span style="font-weight: bold">@Model.PageCount</span></span>
                        if (Model.HasPreviousPage)
                        {
                            @Html.ActionLink("Trang đầu", "Index", new { page = 1, searchString  = search})
                            @Html.Raw(" ")
                            ;
                            @Html.ActionLink("<", "Index", new { page = Model.PageNumber - 1, searchString = search })
                        }

                        if (Model.HasNextPage)
                        {
                            @Html.ActionLink(">", "Index", new { page = Model.PageNumber + 1, searchString = search })
                            @Html.Raw(" ")
                            ;
                            @Html.ActionLink("Trang cuối", "Index", new { page = Model.PageCount, searchString = search })
                        }
                    }                  
                    
                </div>
            </div>
        </div>
        <br class="spacer" />
    </div>
    <br class="spacer"/> 
</div>
<div id="dialog-confirm" title="Xóa dữ liệu?" style="display: none">
    <p>Dữ liệu này sẽ được xóa? Bạn có muốn tiếp tục</p>
</div>